package admin_rule_handler

import (
	admin_rule_service "oc_saas/internal/app/api/admin/rule/service"
	admin_rule_types "oc_saas/internal/app/api/admin/rule/types"
	"oc_saas/internal/model/share"
	"oc_saas/internal/types/api"

	"github.com/gin-gonic/gin"
)

type Rule struct {
	api.Api
}

func (e *Rule) AddRule(c *gin.Context) {
	req := admin_rule_types.Rule{}
	s := &admin_rule_service.Rule{}
	err := e.MakeContext(c).Bind(&req, api.JSON).Valid(&req).MakeOrm().MakeService(&s.Service).Errors
	if err != nil {
		panic(err)
	}
	s.AddRule(&req)
	e.Ok(c)
}

func (e *Rule) GetRuleList(c *gin.Context) {
	req := admin_rule_types.RuleListParamsReq{}
	s := &admin_rule_service.Rule{}
	res := []*share.Rule{}
	err := e.MakeContext(c).Bind(&req, api.QUERY).MakeOrm().MakeService(&s.Service).Errors
	if err != nil {
		panic(err)
	}

	s.GetRuleList(req.RuleType, &res)
	e.OkWithData(&res, c)
}

var RuleApi = new(Rule)
